package com.dhj.jvmstudy.chapter05;

/**
 * 未发生逃逸，栈上分配测试
 * -Xms1G -Xmx1G -XX:-DoEscapeAnalysis -XX:+PrintGCDetails
 */
public class StackAllocation {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();

        for (int i=0; i< 10000000; i++){
            alloc();
        }

        // 查看执行时间
        long end = System.currentTimeMillis();

        /**
         * -XX:-DoEscapeAnalysis 未开启逃逸分析，用时76ms
         * -XX:+DoEscapeAnalysis 开启逃逸分析，用时5ms
         */
        System.out.println("花费的时间："+(end -start)+" ms");

        Thread.sleep(1000000);
    }

    private static void alloc(){
        User user = new User();   // 未发生逃逸
    }

    static class User {

    }

}
